<template>
  <header class="header">
    <h1>todos</h1>
    <input
      v-model="checkAll"
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
    />
    <label for="toggle-all"></label>
    <input
      v-model="content"
      @keyup.enter="addFn"
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
    />
  </header>
</template>

<script>
export default {
  props: {
    arr: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      content: "",
    };
  },
  methods: {
    // 添加内容
    addFn() {
      // 非空判断
      if (this.content.trim() === "") return alert("请输入正确内容");
      this.arr.unshift({
        id: +new Date(),
        name: this.content,
        isDone: false,
      });
      this.content = "";
    },
  },
  computed: {
    // 全选 反选
    checkAll: {
      get() {
        // 解决内容没有时显示全选问题
        if (this.arr.length === 0) return false;
        return this.arr.every((item) => item.isDone);
      },
      set(val) {
        this.arr.forEach((item) => (item.isDone = val));
      },
    },
  },
};
</script>
